import Cal from '@/views/cal.vue'
import Cart from '@/views/cart.vue'
import Home from '@/views/home.vue'
import Index from '@/views/index.vue'
import Lb from '@/views/lb.vue'
import Login from '@/views/login.vue'
import My from '@/views/my.vue'
import Register from '@/views/register.vue'
import Ss from '@/views/ss.vue'
import Xq from '@/views/xq.vue'
import { createRouter, createWebHistory } from 'vue-router'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
      { path: '/',redirect:'/login',},
    { path: '/index',name: 'index', component: Index, children:[
           { path: 'home',name: 'home', component: Home,},
           { path: 'cal',name: 'cal', component: Cal,},
           { path: 'cart',name: 'cart', component: Cart,},
           { path: 'my',name: 'my', component: My,},

    ]},
       { path: '/login',name: 'login', component: Login,},
       { path: '/register',name: 'register', component: Register,},
       { path: '/ss',name: 'ss', component: Ss,},
       { path: '/xq/:goods_id',name: 'xq', component: Xq,},
       { path: '/lb/:query',name: 'lb', component: Lb,},
      

  ],
})

router.beforeEach((to,from,next)=>{
  let token=localStorage.getItem('token');
  if(to.fullPath=='/index/cart' || to.fullPath=='/index/my'){
      if(!token){
        next('/login')
      }
      else{
        next()
      }
  }
  else{
    next()
  }
})

export default router
